package com.vcredit.creditcat.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.framework.pagination.Criteria;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.ChannelStatisticsExternal;
import com.vcredit.creditcat.persistence.base.ChannelStatisticsExternalMapper;
import com.vcredit.creditcat.persistence.base.CountMapper;
import com.vcredit.creditcat.vo.ChanneManVo;

/**
 * 导出
 *
 * @author chengxin
 *
 */
@Service
public class CountService extends BaseService {
	@Autowired
	private CountMapper countMapper;
	@Autowired
	private ChannelStatisticsExternalMapper channelStatisticsExternalMapper;

	/**
	 * 根据不同的条件查询不同数据 1. 总数据 2. 分组数据
	 *
	 * @param request
	 * @param groupBy
	 * @return
	 */
	public List<ChanneManVo> findByTime(HttpServletRequest request, String groupBy) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startTime", request.getParameter("startTime"));
		map.put("endTime", request.getParameter("endTime"));
		map.put("groupByTime", groupBy);
		List<ChanneManVo> chioList = countMapper.find(map);
		return chioList;
	}

	public Page<ChanneManVo> findByPage(Criteria criteria) throws Exception {
		List<ChanneManVo> mpo = countMapper.findByPage(criteria);
		Long li = countMapper.findByPageCount(criteria);
		criteria.toString().split(";");
		return new Page<ChanneManVo>(li, mpo);
	}

	public Page<ChannelStatisticsExternal> findByPages(Criteria criteria) throws Exception {
		List<ChannelStatisticsExternal> mpo = channelStatisticsExternalMapper.findByPage(criteria);
		if (mpo.size() > 0) {
			mpo.add(this.chanel(mpo));
		}
		Long li = channelStatisticsExternalMapper.findByPageCount(criteria);
		criteria.toString().split(";");
		return new Page<ChannelStatisticsExternal>(li, mpo);
	}

	public ChannelStatisticsExternal chanel(List<ChannelStatisticsExternal> mpo) {
		BigDecimal registerNum = BigDecimal.ZERO;
		/** 激活数 **/
		BigDecimal activationNum = BigDecimal.ZERO;
		/** 申请数 **/
		BigDecimal applyNum =  BigDecimal.ZERO;
		/** 提交数 **/
		BigDecimal submitNum =  BigDecimal.ZERO;
		/** 通过数 **/
		BigDecimal adoptNum =  BigDecimal.ZERO;
		/** 绑卡数 **/
		BigDecimal tiecardNum =  BigDecimal.ZERO;
		/** 签约数 **/
		BigDecimal signNum =  BigDecimal.ZERO;
		/** 放款数 **/
		BigDecimal loanAmount =  BigDecimal.ZERO;
		/** 放款金额(元) **/
		BigDecimal loanAmountNum =BigDecimal.ZERO;
		for (ChannelStatisticsExternal chann : mpo) {
			registerNum=registerNum.add(chann.getRegisterNum());
			activationNum=activationNum.add(chann.getActivationNum());
			submitNum=submitNum.add(chann.getSubmitNum());
			adoptNum=adoptNum.add(chann.getAdoptNum());
			tiecardNum=tiecardNum.add(chann.getTiecardNum());
			signNum=signNum.add(chann.getSignNum());
			loanAmount=loanAmount.add(chann.getLoanAmount());
			applyNum=applyNum.add(chann.getApplyNum());
			loanAmountNum=loanAmountNum.add(chann.getLoanAmountNum());
		}
		ChannelStatisticsExternal channelStatisticsExternal = new ChannelStatisticsExternal();
		channelStatisticsExternal.setChannelName("总计");
		/** 注册数 **/
		channelStatisticsExternal.setRegisterNum(registerNum);
		/** 激活数 **/
		channelStatisticsExternal.setActivationNum(activationNum);
		/** 申请数 **/
		channelStatisticsExternal.setApplyNum(applyNum);
		/** 提交数 **/
		channelStatisticsExternal.setSubmitNum(submitNum);
		/** 通过数 **/
		channelStatisticsExternal.setAdoptNum(adoptNum);
		/** 绑卡数 **/
		channelStatisticsExternal.setTiecardNum(tiecardNum);
		/** 签约数 **/
		channelStatisticsExternal.setSignNum(signNum);
		/** 放款数 **/
		channelStatisticsExternal.setLoanAmount(loanAmount);
		/** 放款金额(元) **/
		channelStatisticsExternal.setLoanAmountNum(loanAmountNum);
		channelStatisticsExternal.setStatisticalStartTime("---------");
		channelStatisticsExternal.setStatisticalEndTime("---------");
		return channelStatisticsExternal;
	}

	public Long findByPageCount(Criteria criteria) {
		return null;
	}
}
